package com.jandan.ui.client;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Widget;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.form.Label;
import com.gwtext.client.widgets.layout.HorizontalLayout;
import com.gwtext.client.widgets.layout.VerticalLayout;
import com.jandan.ui.model.LearnedData;
import com.jandan.ui.model.Lesson;
import com.jandan.ui.model.ReviewInfo;

public class StatisticsInfoPanel extends Panel {
	private Lesson lesson;
	private LearnedData learnedData;
	private List<ReviewInfo> reviewInfoList;
	private ILessonOperator lessonOperator;

	public StatisticsInfoPanel(Lesson lesson) {
		// TODO Auto-generated constructor stub
		this.lesson = lesson;
		this.createComponent();
	}

	public StatisticsInfoPanel(Lesson lesson, LearnedData learnedData,
			List<ReviewInfo> reviewInfoList) {
		// TODO Auto-generated constructor stub
		this.lesson = lesson;
		this.learnedData = learnedData;
		this.reviewInfoList = reviewInfoList;
		this.createComponent();
	}

	public void setLessonOperator(ILessonOperator lessonOperator) {
		this.lessonOperator = lessonOperator;
	}

	private void createComponent() {
		this.setAutoScroll(true);
		this.setTitle("统计信息");
		this.setLayout(new VerticalLayout(20));
		this.setPaddings(10, 20, 0, 0);
		BaseInfoPanel baseInfoPanel = new BaseInfoPanel();

		this.add(baseInfoPanel);

		if (reviewInfoList != null && reviewInfoList.size() > 0) {
			final ReviewInfo todayReviewInfo = reviewInfoList.remove(0);
			if (todayReviewInfo != null && todayReviewInfo.getWordCount() > 0) {
				Panel todayPanel = new Panel();
				todayPanel.setLayout(new HorizontalLayout(15));
				Label label = new Label();
				label
						.setText("今天已学习" + todayReviewInfo.getWordCount()
								+ "个单词.");
				label.addClass("info-label");

				Hyperlink link = new Hyperlink("",true,"");
				link.setText("点击复习");
				link.addStyleName("info-label");

				link.addClickListener(new ClickListener() {
					public void onClick(Widget sender) {
						// TODO Auto-generated method stub
						// MessageBox.alert("复习当天所学单词");
						lessonOperator.reviewTodayLearnedWords(lesson);
					}
				});

				todayPanel.add(label);
				todayPanel.add(link);

				this.add(todayPanel);
			}
		}

		if (reviewInfoList != null && reviewInfoList.size() > 0) {
			Panel passPanel = new Panel();
			final List<Date> dateList = new ArrayList<Date>();
			passPanel.setLayout(new HorizontalLayout(15));

			Label label = new Label();
			label.addClass("info-label");
			DateTimeFormat fmt = DateTimeFormat.getFormat("yyyy-MM-dd");
			StringBuffer sb = new StringBuffer("建议复习");
			int size = reviewInfoList.size(); 
			ReviewInfo r = null;
			Date d = null;
			for (int i = 0; i < size; i++) {
				r = reviewInfoList.get(i);
				d = r.getDate();
				dateList.add(d);
				sb.append(fmt.format(d));
				sb.append("(");
				sb.append(r.getWordCount());
				sb.append(")");
				if (i != reviewInfoList.size() - 1) {
					sb.append(",");
				}
			}
			sb.append("所学单词");

			label.setText(sb.toString());

			Hyperlink link = new Hyperlink();
			link.setText("点击复习");
			link.addStyleName("info-label");
			link.addClickListener(new ClickListener() {

				public void onClick(Widget arg0) {
					// TODO Auto-generated method stub
					lessonOperator.reviewLearnedWords(lesson, dateList);
				}
			});

			passPanel.add(label);
			passPanel.add(link);
			this.add(passPanel);
		}
	}

	private class BaseInfoPanel extends Panel {
		public BaseInfoPanel() {
			createComponent();
		}

		private void createComponent() {
			DateTimeFormat fmt = DateTimeFormat
					.getFormat("yyyy-MM-dd hh:mm:ss");

			this.setLayout(new VerticalLayout(10));

			Label lessonLabel = new Label();
			lessonLabel.setText("课程名:" + lesson.getLessonName());
			lessonLabel.addClass("info-label");
			this.add(lessonLabel);

			Label wordLibNameLabel = new Label();
			wordLibNameLabel.setText("单词总数:"
					+ lesson.getWordLib().getWordLibName());
			wordLibNameLabel.addClass("info-label");
			this.add(wordLibNameLabel);

			Label wordLibTotalCountLabel = new Label();
			wordLibTotalCountLabel.setText("单词总数:"
					+ lesson.getWordLib().getTotalWordCount());
			wordLibTotalCountLabel.addClass("info-label");
			this.add(wordLibTotalCountLabel);

			Label smallWordLibCountLabel = new Label();
			smallWordLibCountLabel.setText("小词库个数:"
					+ lesson.getSmallWordLibList().size());
			smallWordLibCountLabel.addClass("info-label");
			this.add(smallWordLibCountLabel);

			Label wordsCountOfPerSmallWordLibLabel = new Label();
			wordsCountOfPerSmallWordLibLabel.setText("小词库单词数:"
					+ lesson.getWordsCountOfPerSmallLib());
			wordsCountOfPerSmallWordLibLabel.addClass("info-label");
			this.add(wordsCountOfPerSmallWordLibLabel);

			Label startLearnedDateLabel = new Label();
			startLearnedDateLabel.setText("创建时间:"
					+ fmt.format(lesson.getCreatedDate()));
			startLearnedDateLabel.addClass("info-label");
			this.add(startLearnedDateLabel);

			if (lesson.getLatestLearnedDate() != null) {
				Label latestLearnedDateLabel = new Label();
				latestLearnedDateLabel.setText("最近学习时间:"
						+ fmt.format(lesson.getLatestLearnedDate()));
				latestLearnedDateLabel.addClass("info-label");
				this.add(latestLearnedDateLabel);
			}
			
			Panel p = new Panel();
			p.setHtml(getStudyData());
			
			this.add(p);
		}
	}

	private String getStudyData() {
		int a = learnedData.getTotalLearnedWordCount();
		int b = learnedData.getTotalMasterWordCount();
		int c = learnedData.getTotalKnowWordCount();
		int d = learnedData.getTotalUnknowWordCount();

		int i = a - b - c - d;

		float f = 0;
		if (lesson.getWordLib().getTotalWordCount() != 0) {
			f = (a * 100) / lesson.getWordLib().getTotalWordCount();
		}
		
		StringBuffer sb = new StringBuffer();
		sb.append("<table class='info-statistic'>");
		sb.append("<tr><td>已学单词数</td><td>掌握单词数</td><td>认识单词数</td><td>陌生单词数</td><td>未标记单词数</td><td>学习进度(%)</td></tr>");
		sb.append("<tr><td>");
		sb.append(a);
		sb.append("</td><td>");
		sb.append("<tr><td>");
		sb.append(b);
		sb.append("</td><td>");
		sb.append("<tr><td>");
		sb.append(c);
		sb.append("</td><td>");
		sb.append("<tr><td>");
		sb.append(d);
		sb.append("</td><td>");
		sb.append("<tr><td>");
		sb.append(i);
		sb.append("</td><td>");
		sb.append("<tr><td>");
		sb.append(f);
		sb.append("</td><td>");
		sb.append("</table>");
		
		return sb.toString();
	}
}
